package com.hpq.user.service.adapter;

import com.hpq.user.domain.enums.ApplyReadStatusEnum;
import com.hpq.user.domain.enums.ApplyStatusEnum;
import com.hpq.user.domain.enums.ApplyTypeEnum;
import com.hpq.user.domain.model.User;
import com.hpq.user.domain.model.UserApply;
import com.hpq.user.domain.model.UserFriend;
import com.hpq.user.domain.vo.request.friend.FriendApplyReq;
import com.hpq.user.domain.vo.response.friend.FriendApplyResp;
import com.hpq.user.domain.vo.response.friend.FriendResp;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *  好友列表适配器
 */
public class FriendAdapter {
    /**
     *  构建好友列表
     * @param data
     * @param userList
     * @return
     */
    public static List<FriendResp> buildFriendResp(List<UserFriend> data, List<User> userList) {
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));
        return data.stream()
                .map(userFriend -> {
                    FriendResp friendResp = new FriendResp();
                    friendResp.setUid(userFriend.getFriendUid());
                    User user = userMap.get(userFriend.getFriendUid());
                    if(Objects.nonNull(user)){
                        friendResp.setActiveStatus(user.getActiveStatus());
                        friendResp.setAvatar(user.getAvatar());
                        friendResp.setName(user.getName());
                    }
                    return friendResp;
                }).collect(Collectors.toList());
    }

    /**
     *  构建好友申请
     * @param uid
     * @param req
     * @return
     */
    public static UserApply buildFriendApply(Long uid, FriendApplyReq req) {
        return UserApply.builder()
                .msg(req.getMsg())
                .targetId(req.getTargetUid())
                .uid(uid)
                .type(ApplyTypeEnum.ADD_FRIEND.getCode())
                .status(ApplyStatusEnum.WAIT_APPROVAL.getCode())
                .readStatus(ApplyReadStatusEnum.UNREAD.getCode())
                .build();
    }

    /**
     *  构建好友申请列表
     * @param records
     * @return
     */
    public static List<FriendApplyResp> buildFriendApplyResp(List<UserApply> records) {
        return records.stream().map(record ->{
            FriendApplyResp applyResp = new FriendApplyResp();
            applyResp.setApplyId(record.getId());
            applyResp.setUid(record.getUid());
            applyResp.setType(record.getType());
            applyResp.setMsg(record.getMsg());
            applyResp.setStatus(record.getStatus());
            return applyResp;
        }).collect(Collectors.toList());
    }
}
